#include "InputComponentManager.h"



void RInputComponentManager::RegisterComponent(TSharedObjectPtr<RInputComponent> InComponent)
{
    InputComponents.Add(InComponent);
}

void RInputComponentManager::UnregisterComponent(TSharedObjectPtr<RInputComponent> InComponent)
{
    InputComponents.Remove(InComponent);
}

bool RInputComponentManager::OnViewportMouseButtonPressed(const SMouseButtonEvent &InMouseEvent, TSharedObjectPtr<RViewportWidget> InViewport)
{
    for(auto& Component:InputComponents)
    {
        bool bCanAccept=true;
        if(Component->CanAccept.IsBound())
        {
            bCanAccept=Component->CanAccept.Execute(InViewport);
        }

        if(!bCanAccept)
        {
            continue;
        }

        if(Component->OnViewportMouseButtonPressed(InMouseEvent))
        {
            return true;
        }
    }

    return false;
}

bool RInputComponentManager::OnViewportMouseButtonReleased(const SMouseButtonEvent &InMouseEvent, TSharedObjectPtr<RViewportWidget> InViewport)
{
    for(auto& Component:InputComponents)
    {
        bool bCanAccept=true;
        if(Component->CanAccept.IsBound())
        {
            bCanAccept=Component->CanAccept.Execute(InViewport);
        }

        if(!bCanAccept)
        {
            continue;
        }

        if(Component->OnViewportMouseButtonReleased(InMouseEvent))
        {
            return true;
        }
    }

    return false;
}

bool RInputComponentManager::OnViewportMouseMoved(const SMouseButtonEvent &InMouseEvent, TSharedObjectPtr<RViewportWidget> InViewport)
{
    for(auto& Component:InputComponents)
    {
        bool bCanAccept=true;
        if(Component->CanAccept.IsBound())
        {
            bCanAccept=Component->CanAccept.Execute(InViewport);
        }

        if(!bCanAccept)
        {
            continue;
        }

        if(Component->OnViewportMouseMoved(InMouseEvent))
        {
            return true;
        }
    }

    return false;
}

bool RInputComponentManager::ProcessKeyboardEvent(const SKeyboardEvent &BoardkeyEvent, TSharedObjectPtr<RViewportWidget> InViewport)
{
    for(auto& Component:InputComponents)
    {
        bool bCanAccept=true;
        if(Component->CanAccept.IsBound())
        {
            bCanAccept=Component->CanAccept.Execute(InViewport);
        }

        if(!bCanAccept)
        {
            continue;
        }

        if(Component->ProcessKeyboardEvent(BoardkeyEvent))
        {
            return true;
        }
    }

    return false;
}
